package com.winz.cultureapp.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.zxing.WriterException;
import com.terry.zxing.zxing.encode.CodeCreator;
import com.winz.cultureapp.R;
import com.winz.cultureapp.base.BaseAppCompatActivity;
import com.winz.cultureapp.constants.Constants;
import com.winz.cultureapp.entity.BookFixBean;
import com.winz.cultureapp.entity.EventBusBean;
import com.winz.cultureapp.entity.PersonItem2ListBean;
import com.winz.cultureapp.http.OnResponeListener;
import com.winz.cultureapp.http.request.ActivityBookCancleReq;
import com.winz.cultureapp.http.util.HttpUitls;
import com.winz.cultureapp.utils.JsonUtil;
import com.winz.cultureapp.utils.Logger;
import com.winz.cultureapp.utils.SharePrefsUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 活动订单详情
 */
public class ActivtiyOrderDesActivity extends BaseAppCompatActivity {
    @BindView(R.id.tv_top_title)
    TextView mTv_top_title;
    @BindView(R.id.tv_order_type)
    TextView mTv_order_type;
    @BindView(R.id.tv_no)
    TextView mTv_no;
    @BindView(R.id.tv_order_time)
    TextView mTv_order_time;
    @BindView(R.id.iv_pic)
    ImageView mIv_pic;
    @BindView(R.id.tv_price)
    TextView mTv_price;
    @BindView(R.id.tv_title)
    TextView mTv_title;
    @BindView(R.id.tv_location)
    TextView mTv_location;
    @BindView(R.id.tv_time)
    TextView mTv_time;
    @BindView(R.id.tv_seat)
    TextView mTv_seat;
    @BindView(R.id.tv_type)
    TextView mTv_type;
    @BindView(R.id.rl_code)
    RelativeLayout mRl_code;
    @BindView(R.id.iv_code)
    ImageView mIv_code;
    @BindView(R.id.iv_code_2)
    ImageView mIv_code_2;

    private PersonItem2ListBean.DataBean.ListBean mData;

    @Override
    protected int contentView() {
        return R.layout.activity_activity_order_des;
    }

    @Override
    protected void initViews() {
        setStatusBar(getResources().getColor(R.color.base_color));
        mTv_top_title.setText("订单详情");

    }

    @Override
    protected void loadDatas(Intent intent) {
        if (intent != null){
            if (intent.getExtras() != null){
                mData = (PersonItem2ListBean.DataBean.ListBean) intent.getExtras().getSerializable(Constants.KEY.RECORD.RECORD_DATA);
                if (mData != null){
                    updateUi();
                }
            }
        }
    }


    private void updateUi() {
        mTv_order_type.setText("已付款");
        mTv_no.setText("订单号：" + mData.orderNumber);
        mTv_order_time.setText("订单时间：" + mData.createTime);
        mTv_price.setText("￥ " + mData.seatMoney);
        mTv_title.setText(mData.activityName);

        mTv_location.setText("地址：" + mData.address);
        mTv_time.setText("时间：" + mData.startTime.substring(0,16) + " - " + mData.endTime.substring(0,16) );
        mTv_seat.setText("座位：" + (TextUtils.isEmpty(mData.seatDesc)?"无" : mData.seatDesc));

        //圆角处理
        Glide.with(this)
                .load(mData.imageUrl)
                .error(R.drawable.pic_default)
                .into(mIv_pic);
        Bitmap codeImage = null;
        try {
            codeImage = CodeCreator.createQRCode("flag=0&str="+mData.randomStr); // flag=0表示场馆活动
            mIv_code.setImageBitmap(codeImage);
            mIv_code_2.setImageBitmap(codeImage);
        } catch (WriterException e) {
            e.printStackTrace();
        }

        mTv_type.setText(mData.useStatus == 0?"未使用":mData.useStatus == 1?"已使用":"已过期");
        mTv_type.setTextColor(getResources().getColor(mData.useStatus == 0?R.color.base_color:mData.useStatus == 1?R.color.dodgerblue:R.color.icon_red));
    }

    @OnClick({R.id.fl_back, R.id.iv_code, R.id.tv_cancle,R.id.tv_close})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_back:
                finish();
                break;
            case R.id.iv_code:
                mRl_code.setVisibility(View.VISIBLE);
                break;
            case R.id.tv_cancle:
                if (mData.useStatus == 0){
                    onCancle();
                }else {
                    showMsg((mData.useStatus == 1?"已使用":"已过期")+"的订单不可取消");
                }

                break;
            case R.id.tv_close:
                EventBus.getDefault().post(new EventBusBean(4,0,""));
                mRl_code.setVisibility(View.GONE);
                break;
        }
    }

    public void onCancle() {
        showProgressDialog();
        HttpUitls.onPostByToken("activity_book_cancel",
                SharePrefsUtil.getInstance().getString(Constants.USER_INFO.TOKEN),
                new ActivityBookCancleReq(mData.activitySeatRecordId),
                new OnResponeListener<BookFixBean>() {
                    @Override
                    public void onSuccess(BookFixBean bean) {
                        Logger.d("activity_book_cancel==>>", JsonUtil.getJsonFromObj(bean));
                        HttpUitls.isExpire(bean.code);
                        if (bean.code == 0) {
                            showMsg("取消预定成功");
                            finish();
                        } else {
                            showMsg(bean.msg);
                        }

                        dismissProgressDialog();
                    }

                    @Override
                    public void onFailed(String error) {
                        dismissProgressDialog();
                        Logger.d("activity_book_cancel==>>", error);
                    }
                });
    }
}
